using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace PSErrorLogToDB
{
    public partial class Filter : Form
    {
        private string connectionString;

        private void Filter_Load(object sender, EventArgs e)
        {
            DataSet dsFilter = new DataSet();
            using (SqlConnection cn = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("GetFilterValues", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cn.Open();

                SqlDataReader reader = cmd.ExecuteReader();
                string[] tables = new string[5];
                tables[0] = "Area";
                tables[1] = "Category";
                tables[2] = "Level";
                tables[3] = "Process";
                tables[4] = "Server";
                dsFilter.Load(reader, LoadOption.OverwriteChanges, tables);
                reader.Close();
            }
            cbAreaFilter.Items.Clear();
            foreach (DataRow dr in dsFilter.Tables["Area"].Rows)
            {
                cbAreaFilter.Items.Add(dr["Area"].ToString().Trim());
            }
            if (cbAreaFilter.Items.Count > 0 && cbAreaFilter.Text == "") cbAreaFilter.SelectedIndex = 0; 
            cbCatFilter.Items.Clear();
            foreach (DataRow dr in dsFilter.Tables["Category"].Rows)
            {
                cbCatFilter.Items.Add(dr["Category"].ToString().Trim());
            }
            if (cbCatFilter.Items.Count > 0 && cbCatFilter.Text == "") cbCatFilter.SelectedIndex = 0;
            cbLevelFilter.Items.Clear();
            foreach (DataRow dr in dsFilter.Tables["Level"].Rows)
            {
                cbLevelFilter.Items.Add(dr["Level"].ToString().Trim());
            }
            if (cbLevelFilter.Items.Count > 0 && cbLevelFilter.Text == "") cbLevelFilter.SelectedIndex = 0;
            cbProcessFilter.Items.Clear();
            foreach (DataRow dr in dsFilter.Tables["Process"].Rows)
            {
                cbProcessFilter.Items.Add(dr["Process"].ToString().Trim());
            }
            if (cbProcessFilter.Items.Count > 0 && cbProcessFilter.Text == "") cbProcessFilter.SelectedIndex = 0;
            cbServerNameFilter.Items.Clear();
            foreach (DataRow dr in dsFilter.Tables["Server"].Rows)
            {
                cbServerNameFilter.Items.Add(dr["ServerName"].ToString().Trim());
            }
            if (cbServerNameFilter.Items.Count > 0 && cbServerNameFilter.Text == "") cbServerNameFilter.SelectedIndex = 0;
        }

        public Filter()
        {
            InitializeComponent();
        }

        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }

        public bool FilterTimestamp
        {
            get { return !chkNoFilterTS.Checked; }
        }

        public bool FilterServerName
        {
            get { return !chkNoFilterServer.Checked; }
        }

        public bool FilterLevel
        {
            get { return !chkNoFilterLevel.Checked; }
        }

        public bool FilterArea
        {
            get { return !chkNoFilterArea.Checked; }
        }

        public bool FilterCategory
        {
            get { return !chkNoFilterCat.Checked; }
        }

        public bool FilterProcess
        {
            get { return !chkNoFilterProcess.Checked; }
        }

        public DateTime StartRange
        {
            get { return dtStartRange.Value; }
        }

        public DateTime EndRange
        {
            get { return dtEndRange.Value; }
        }

        public string Area
        {
            get { return cbAreaFilter.Text; }
        }

        public string Category
        {
            get { return cbCatFilter.Text; }
        }

        public string Level
        {
            get { return cbLevelFilter.Text; }
        }

        public string Process
        {
            get { return cbProcessFilter.Text; }
        }

        public string ServerName
        {
            get { return cbServerNameFilter.Text; }
        }

        public string Msg
        {
            get { return txtMsg.Text; }
        }

        private void chkNoFilterTS_CheckedChanged(object sender, EventArgs e)
        {
            dtEndRange.Enabled = !chkNoFilterTS.Checked;
            dtStartRange.Enabled = !chkNoFilterTS.Checked;
        }

        private void chkNoFilterServer_CheckedChanged(object sender, EventArgs e)
        {
            cbServerNameFilter.Enabled = !chkNoFilterServer.Checked;
        }

        private void chkNoFilterLevel_CheckedChanged(object sender, EventArgs e)
        {
            cbLevelFilter.Enabled = !chkNoFilterLevel.Checked;
        }

        private void chkNoFilterArea_CheckedChanged(object sender, EventArgs e)
        {
            cbAreaFilter.Enabled = !chkNoFilterArea.Checked;
        }

        private void chkNoFilterCat_CheckedChanged(object sender, EventArgs e)
        {
            cbCatFilter.Enabled = !chkNoFilterCat.Checked;
        }

        private void chkNoFilterProcess_CheckedChanged(object sender, EventArgs e)
        {
            cbProcessFilter.Enabled = !chkNoFilterProcess.Checked;
        }
    }
}